<script setup>
import {
  ref
} from 'vue'
import {CheckTokenAjax, LoginAjax} from "../../apis/api"

/**
 * 数据
 */
const account = ref()
const password = ref()

/**
 * 检测token自动登录
 */
const autoLogin = () => {
  uni.getStorage({
    key: "token",
    success: async (res) => {
      await CheckTokenAjax()
      setTimeout(() => {
        uni.redirectTo({
          url: "/frame/pages/menu/index"
        })
      }, 1000)
    }
  })
}

/**
 * 登录
 */
const handleLogin = async () => {
  const result = await LoginAjax({
    account: account.value,
    password: password.value
  })
  uni.setStorage({
    key: "token",
    data: result,
    success: () => {
      uni.getStorage({
        key: "token",
        success: (res) => {
          if (res.data) {
            uni.showToast({
              title: '登录成功',
              icon: "success",
              musk: true
            })
            setTimeout(() => {
              uni.redirectTo({
                url: "/frame/pages/menu/index"
              })
            }, 1000)
          }
        }
      })
    },
    fail: () => {
      uni.showToast({
        title: '保存登录信息失败，请检查手机储存授权',
        icon: "none"
      })
    }
  })
}
</script>

<template>
  <view class="page-wrap">
    <view class="page-header">
      <image class="header-logo" src="/static/img/logo.jpg"></image>
      <view class="header-title">
        信达仓储管理系统
      </view>
    </view>
    <view class="page-body">
      <view class="body-form">
        <view class="form-item">
          <view class="item-content">
            账号
          </view>
          <input v-model="account" class="item-input" placeholder="账号" placeholder-class="placeholder" type="text"/>
        </view>
        <view class="form-item">
          <view class="item-content">
            密码
          </view>
          <input v-model="password" class="item-input" placeholder="密码" placeholder-class="placeholder"
                 type="password"/>
        </view>
        <view class="form-btn">
          <view class="btn-content" @click="handleLogin">
            登录
          </view>
        </view>
      </view>
    </view>
    <view class="page-footer">
      <view class="footer-version">
        当前版本：1.0.0
      </view>
    </view>
  </view>
</template>

<style scoped src="./index.scss">

</style>